<?php


interface Log
{
    public function write();
}

//文件记录操作日志

class FileLog implements Log
{

    public function write()
    {
        // TODO: Implement write() method.
    }
}

//数据库操作日志
class DatabaseLog implements Log
{
    public function write()
    {
        // TODO: Implement write() method.
    }
}

class User
{
    protected $log;

    /**
     * User constructor.
     * @param $log
     */
    public function __construct(Log $log)
    {
        $this->log = $log;
    }

    public function login()
    {
        echo 'login success';
        $this->log->write();
    }
}

$user = new User(new DatabaseLog());

function make($concert)
{
    $reflector = new \ReflectionClass($concert);
    $constructor = $reflector->getConstructor();
    if (is_null($constructor)) {
        return $reflector->newInstance();
    }
    $dependencies = $constructor->getParameters();
    $instance = getDependencies($dependencies);
    return $reflector->newInstanceArgs($instance);
}

function getDependencies($parameters)
{
    $dependencies = [];
    foreach ($parameters as $parameter) {
        $dependencies[] = make($parameter->getClass()->name);
    }
    return $dependencies;
}
$user = make(User::class);
$user->login();







